/**  
 * @Title: TransServletTest.java
 * @Package com.icss.trans.accept.httppush
 * @Description: 
 * @author ICSS
 * @date 2015-3-26 下午1:23:54
 * @version V1.0  
 */ 
package com.icss.trans.accept.httppush;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


/**
 * @ClassName: TransServletTest
 * @Description:
 * @author ICSS
 * @date 2015-3-26 下午1:23:54
 *
 */
public class TransServletTest {

    /**
     * @Description:
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
    }

    /**
     * @Description: 
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }

    /**
     * Test method for {@link com.icss.trans.accept.httppush.TransServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
     */
    @Test
    public void testDoPostHttpServletRequestHttpServletResponse() {
        HttpClient httpclient = null;
        HttpPost post = null;

        try {
            httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
            String sendUrl = "http://localhost:8080/dtcp";
            post = new HttpPost(sendUrl + "/transEntrance");
            HttpResponse response = null;

            MultipartEntity reqEntity = new MultipartEntity();
            addPart(reqEntity, "fileName", "11.xml");
            addPart(reqEntity, "dataTransType", "test001");
            addPart(reqEntity, "sourceAppCode", "ICSS_ETL");
            addPart(reqEntity, "destAppCode", "ICSS_ETL");
            addPart(reqEntity, "sourceOrgCode", "10000001");
            addFilePart(reqEntity, "inputStream", new File("E:/test/1.xml"));
            post.setEntity(reqEntity);

            response = httpclient.execute(post);
            int resultCode = response.getStatusLine().getStatusCode();
            String resultStr = EntityUtils.toString(response.getEntity());

            System.out.println(resultCode + "----" + resultStr);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
            httpclient.getConnectionManager().shutdown();
            post = null;
            httpclient = null;
        }
    }

    /**
     * @Description:
     * @param reqEntity
     */
    private void addFilePart(MultipartEntity reqEntity, String param, File value) {
        if (value != null) {
            reqEntity.addPart(param, new FileBody(value));
        }
    }

    private void addPart(MultipartEntity reqEntity, String param, String value) throws UnsupportedEncodingException {
        if (StringUtils.isNotEmpty(value)) {
            reqEntity.addPart(param, new StringBody(value));
        }
    }
}
